package com.echat.messagesender.sender.service.impl;

import com.echat.messagesender.sender.bean.EchatSendEmailInfo;
import com.echat.messagesender.sender.bean.EchatSendSmsInfo;
import com.echat.messagesender.sender.bean.SendResult;
import com.echat.messagesender.sender.mapper.EmailSendLogDetailMapper;
import com.echat.messagesender.sender.mapper.EmailSendLogMaaper;
import com.echat.messagesender.sender.mapper.SmsSendLogDetailMapper;
import com.echat.messagesender.sender.mapper.SmsSendLogMapper;
import com.echat.messagesender.sender.model.EmailSendLog;
import com.echat.messagesender.sender.model.EmailSendLogDetail;
import com.echat.messagesender.sender.model.SmsSendLog;
import com.echat.messagesender.sender.model.SmsSendLogDetail;
import com.echat.messagesender.sender.service.SendLogService;
import com.echat.messagesender.sender.util.IdGenerator;
import com.echat.messagesender.sender.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * Created by chenmohan on 2018/09/06
 *
 * @description
 */
@Service
public class SendLogServiceImpl implements SendLogService {

    @Autowired EmailSendLogMaaper emailSendLogMaaper;
    @Autowired EmailSendLogDetailMapper emailSendLogDetailMapper;
    @Autowired SmsSendLogMapper smsSendLogMapper;
    @Autowired SmsSendLogDetailMapper smsSendLogDetailMapper;
    @Autowired IdGenerator idGenerator;

    @Transactional
    @Override
    public Long addFullEmailSendLog(EchatSendEmailInfo info) {
        //添加"email_sendlog"表
        Long emailId = idGenerator.genId();
        EmailSendLog sendLog = EmailSendLog.builder()
                .id(emailId)
                .userId(info.getUserId())
                .receivers(StringUtils.joinStringArrayWithComma(info.getReceivers()))
                .sendTime(LocalDateTime.now())
                .sendResult(false).build(); //默认发送失败，发送成功会修改该值
        emailSendLogMaaper.insertSelective(sendLog);

        //添加"email_sendlog_detail"表
        EmailSendLogDetail sendLogDetail = EmailSendLogDetail.builder()
                .emailId(emailId)
                .title(info.getTitle())
                .content(info.getContent())
                .templateId(info.getTemplateId())
                .ccReceivers(StringUtils.joinStringArrayWithComma(info.getCcReceivers()))
                .bccReceivers(StringUtils.joinStringArrayWithComma(info.getBccReceivers()))
                .replayEmail(info.getReplyEmail())
                .vars(info.getVars()).build();
        emailSendLogDetailMapper.insertSelective(sendLogDetail);
        return emailId;
    }

    @Transactional
    @Override
    public Long addFullSmsSendLog(EchatSendSmsInfo info) {
        //添加"email_sendlog"表
        Long smsId = idGenerator.genId();
        SmsSendLog sendLog = SmsSendLog.builder()
                .id(smsId)
                .userId(info.getUserId())
                .recipient(StringUtils.joinStringArrayWithComma(info.getPhonoNos()))
                .sendTime(LocalDateTime.now())
                .sendResult(false).build();  //默认发送失败，发送成功会修改该值
        smsSendLogMapper.insertSelective(sendLog);

        //添加"email_sendlog_detail"表
        SmsSendLogDetail sendLogDetail = SmsSendLogDetail.builder()
                .smsId(smsId)
                .content(info.getSmsContent())
                .templateId(info.getTemplateId())
                .vars(info.getVars()).build();
        smsSendLogDetailMapper.insertSelective(sendLogDetail);
        return smsId;
    }

    @Override
    public void setEmailSendResult(SendResult result) {
        EmailSendLog updateSendLog = EmailSendLog.builder()
                .id(result.getId())
                .sendResult(result.getSendResult())
                .sendPlatformId(result.getSendPlatformId())
                .memo(result.getMemo()).build();
        emailSendLogMaaper.updateByPrimaryKeySelective(updateSendLog);

    }

    @Override
    public void setSmsSendResult(SendResult result) {
        SmsSendLog updateSendLog = SmsSendLog.builder()
                .id(result.getId())
                .sendResult(result.getSendResult())
                .sendPlatformId(result.getSendPlatformId())
                .memo(result.getMemo()).build();
        smsSendLogMapper.updateByPrimaryKeySelective(updateSendLog);
    }
}
